#include <lpc23xx.h>
#include "LCD.h"
#define DMA_SRC			0x7FD00000
#define DMA_DST			0x7FD00500
#define DMA_SIZE		0x100
#define DMA_Item    0x7FD00700
unsigned int i;
unsigned int *Buffer1,*Buffer2,*Item;

int main (void)
{


       lcd_init();					   //
	lcd_clear();				  //
	set_cursor(0,0);			   // Agaf=/
    lcd_print("MtoM DMA Example");	 //
   T0TCR 		= 0x00000002;			           //Reset counter and prescaler
   Buffer1 = (unsigned int *)DMA_SRC;    //Set the start address of the source buffer
	 Buffer2 = (unsigned int *)DMA_DST;    //Set the start address of the destination buffer
	 
	 for ( i = 0; i < DMA_SIZE/4; i++ )    //Initilise the buffers
	 {
		*Buffer1 = i;
		*Buffer2 = 0;
		Buffer1++;
		Buffer2++;
	}
  	PCONP |= (0x1<<29);                      // Power up the GPDMA
	GPDMA_CONFIG = 0x01;	                   // Enable the GPDMA
	while ( !(GPDMA_CONFIG & 0x01) );        // Wait until the GPDMA is operational
	 
  GPDMA_INT_TCCLR = 0x01;                   //Clear the interrupt status bits
  GPDMA_INT_ERR_CLR = 0x01;
  GPDMA_CH0_SRC = DMA_SRC;                 //Load buffer1 start address into Channel 0
	GPDMA_CH0_DEST = DMA_DST;                //Load buffer2 start address into Channel0
	GPDMA_CH0_CTRL = ((DMA_SIZE/4) & 0x0FFF) //set the transfer size
	               | (0x04 << 12)            // Source burst size 
	               | (0x04 << 15)            //destination burst size  
			           | (0x02 << 18)            //Source width
			           | (0x02 << 21)            //destination width
			           | (1 << 26)            //Source increment
			           | (1 << 27) 
			           | (1<<31);              //Destination increment
		T0TCR 		   = 0x00000001;			
  //enable timer
	GPDMA_CH0_CFG |= 0x08001;                //Start the channel zero transfer
	
	while ( !GPDMA_RAW_INT_TCSTAT );          //Wait until the transfer has finished
	T0TCR 		= 0x00000000;			             //disable timer
	T0TCR 		= 0x00000002;			             //Reset counter and prescaler
    Buffer1 = (unsigned int *)DMA_SRC;     //reset the buffers for a CPU copy
  Buffer2 = (unsigned int *)DMA_DST;
	 for ( i = 0; i < DMA_SIZE/4; i++ )
	 {
		*Buffer1 = i+100;
		*Buffer2 = 0;
		Buffer1++;
		Buffer2++;
	}

	T0TCR 		= 0x00000001;                 //Enable the timer
	
	 Buffer1 = (unsigned int *)DMA_SRC;
	 Buffer2 = (unsigned int *)DMA_DST;
	 for ( i = 0; i < DMA_SIZE/4; i++ )
	 {
		*Buffer2 = *Buffer1;
		Buffer1++;
		Buffer2++;
	}
    T0TCR 		= 0x00000000;			        //disable timer
    
	while(1)
	   {
	       ;
	   }

return (0);    
}